package com.lqwu.mallchatserver.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lqwu.mallchatserver.user.mapper.RoleMapper;
import com.lqwu.mallchatserver.user.model.entry.Role;
import com.lqwu.mallchatserver.user.model.enums.RoleEnum;
import com.lqwu.mallchatserver.user.service.RoleService;
import com.lqwu.mallchatserver.user.service.cache.UserCache;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;

/**
* @author 伍立权
* @description 针对表【role(角色表)】的数据库操作Service实现
* @createDate 2025-05-29 11:11:32
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService {
    @Resource
    private UserCache userCache;

    /**
     * 判断用户是否有权限
     * @param uid
     * @param roleId
     * @return
     */
    @Override
    public Boolean hasPower(Long uid, Long roleId) {
        // 1.从缓存获取用户角色id列表
        Set<Long> roleSet = userCache.getRoleSet(uid);
        // 2.判断用户角色列表中是否包含该角色id/为超级管理员
        return isAdmin(roleSet) || roleSet.contains(roleId);
    }

    private Boolean isAdmin(Set<Long> roleSet) {
        return roleSet.contains(RoleEnum.ADMIN.getId());
    }
}




